package com.appkubes.base.core.utils;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class KylinMbpGenerator {
    //Pkg设置
    String parentPkg = "com.appkubes.portal.core";
    String controllerPkg = "controller";
    String servicePkg = "service";
    String daoPkg = "dao";
    String entityPkg = "entity";
    String poPkg = "po";
    String pojoPkg = "pojo";
    String serviceImplPkg = "service.impl";

    //数据库设置
    String jdbcDriverName = "com.mysql.cj.jdbc.Driver";
    String jdbcUrl = "jdbc:mysql://db.test.appkubes.com:3306/kylin_portal";
    String mysqlUserName = "portal";
    String mysqlPassword = "portal@2020";


    public void execute() {

        String[] tableName = new String[]{"portal_app_admin"};
        String excludeTable = "portal_test";

        AutoGenerator mpg = new AutoGenerator();
        String prefix = "Base%s";

        String projectPath = System.getProperty("user.dir");

        // 全局配置
        GlobalConfig gc = new GlobalConfig()
                .setOutputDir(projectPath + "/src/main/java")// 生成文件的输出目录
                .setAuthor("Rongjie")// 作者
                .setOpen(false)// 生成后打开目录
                .setIdType(IdType.UUID) //使用UUID生成键
                .setDateType(DateType.ONLY_DATE) // 设置时间类型使用哪个包下的
                .setMapperName(prefix + "Dao")  // 文件命名方式
                .setEntityName("%s") //对应数据库的表名
                .setControllerName(prefix + "Controller")
                .setServiceName(prefix + "Service")
                .setServiceImplName(prefix + "ServiceImpl")
                .setFileOverride(true) //覆盖文件
                .setSwagger2(true);  //实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig()
                .setDriverName(jdbcDriverName)
                .setUsername(mysqlUserName)//?serverTimezone=GMT%2B8
                .setPassword(mysqlPassword)
                .setUrl(jdbcUrl);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig()
                .setParent(parentPkg)
                .setEntity(entityPkg)
                .setMapper(daoPkg)
                .setController(controllerPkg)
                .setService(servicePkg)
                .setServiceImpl(serviceImplPkg);
        mpg.setPackageInfo(pc);

        //配置自定义属性注入
        InjectionConfig injectionConfig = new InjectionConfig() {
            //.vm模板中，通过${cfg.abc}获取属性
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("parentPkg", parentPkg);
                map.put("poPkg", parentPkg + "." + poPkg);
                map.put("entityPkg", parentPkg + "." + entityPkg);
                map.put("pojoPkg", parentPkg + "." + pojoPkg);
                map.put("daoPkg", parentPkg + "." + daoPkg);
                map.put("servicePkg", parentPkg + "." + servicePkg);
                map.put("serviceImplPkg", parentPkg + "." + serviceImplPkg);
                this.setMap(map);
            }
        };
        mpg.setCfg(injectionConfig);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig()
                .setController("/templates/kylin.controller.java")
                .setService("/templates/kylin.service.java")
                .setServiceImpl("/templates/kylin.service.impl.java")
                .setMapper("/templates/kylin.dao.java")
                .setXml(null)
                .setEntity("/templates/kylin.entity.java"); // 使用自定义模板，不想要生成就设置为null,如果不设置null会使用默认模板
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig()
                .setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                // 使用lombok
                .setEntityLombokModel(true)
//                .setInclude(tableName)
                .setExclude(excludeTable)
                .setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);

        mpg.execute();
    }


}
